var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
$CertusFormAjax.ajax('initDepts',{},function (response) {
    var data = response.data;
    $$scope.depts = data;
    if (data.length==1){
        $("#deptCode").attr("disabled","disabled");
    }
    $$scope.deptCode = data[0].departmentCode;
});

$CertusFormAjax.ajax("findSeProList",{},function(response){
    var nhlTeams = [];
    var data = response.data;
    for (var i=0;i<data.length;i++){
        var arr={};
        arr.name=data[i].proName;
        arr.id = data[i].id;
        nhlTeams.push(arr);
    }
    //自动补全
    var nhl = $.map(nhlTeams, function (obj) {
        return {
            value: obj.name,
            data: { category: obj.id }
        };
    });
    // Initialize autocomplete with local lookup:
    $('#proName').devbridgeAutocomplete({
        lookup: nhl,
        minChars: 1,
        onSelect: function (suggestion) {
            $("#proId").val(suggestion.data.category);
        },
        showNoSuggestionNotice: true,
        noSuggestionNotice: 'Sorry, no matching results'
    });

});

$CertusFormAjax.ajax("findJbWorkList",{"processId":processId}, function(result){
    var data = result.data;
    $$scope.createUser = data.createUser;
    $$scope.proName = data.proName;
    $$scope.proId = data.proId;
    $$scope.beginTime = data.beginTime;
    $$scope.start = data.start;
    $$scope.endTime = data.endTime;
    $$scope.end = data.end;
    $$scope.vacationDay = data.vacationDay;
    $$scope.totalHour = data.totalHour;
    $$scope.vacationHour = data.vacationHour+"";
    $$scope.comments = data.comments;
    $$scope.opinionMsg = data.opinionMsg;
    $$scope.id = data.id;
});
this.calcHours = function (days) {
    var vacationHour = $("#vacationHour").val();
    if (days == "") {
        return;
    }
    var totalHours = Number(days * 8) + Number(vacationHour);
    $("#totalHour").val(totalHours);
};

this.calcueDay = function (hours) {
    var vacationDay = $("#vacationDay").val();
    if (vacationDay == "") {
        $("#totalHour").val("");
        return;
    }
    var totalHours = Number(vacationDay * 8) + Number(hours);
    $("#totalHour").val(totalHours);
};

//系统计算两日期相差多少个小时
this.getInervalHour=function(startDate, endDate) {
    var d1 = new Date(startDate);
    var d2 = new Date(endDate);
    return parseInt(d2 - d1) / 1000 / 3600+24;
};

$(".submit").on('click',function () {
    // 获取开始日期时间
    var startDay = $("#beginTime").val();
    // 获取结束日期时间
    var endDay = $("#endTime").val();
    if (startDay == "" || startDay == null || startDay == undefined) {
        layer.msg("开始时间不能为空！");
        return;
    }
    if (endDay == "" || endDay == null || endDay == undefined) {
        layer.msg("结束时间不能为空！");
        return;
    }

    $CertusFormAjax.ajax('validWorkDay',{"beginTime":startDay,"endTime":endDay},function (result) {
        if (result.data!=""){
            layer.open({
                title: '提示'
                ,content: result.data+"为工作日，不能进行加班申请"
            });
            return;
        }else{
            var proId = $("#proId").val();
            var comments = $("#comments").val();
            var vacationDay = $("#vacationDay").val();
            var vacationHour = $("#vacationHour").val();

            var startTime = $("#start option:selected").val();
            var startDT = startDay + " " + startTime;

            var endTime = $("#end option:selected").val();
            var endDT = endDay + " " + endTime;

            if (proId==""){
                layer.msg("请选择归属项目！");
                return;
            }

            var diffTHours = getInervalHour(startDT,endDT);
            var diffDay = parseInt(diffTHours/24);
            // var diffChours = diffTHours%24;

            if ($("#totalHour").val() == "0") {
                layer.msg("请假总时长不能为0");
                return;
            }

            // if (diffDay!=vacationDay){
            //     layer.msg("请假总时长填写不正确，请重新填写！正确日期为："
            //         +diffDay+"天");
            //     return;
            // }

            if (comments == "" || comments == null || comments == undefined) {
                layer.msg("请假原因不能为空！");
                return;
            }

            // 转换开始标准时间
            var start = new Date(startDT);
            // 转换结束标准时间
            var end = new Date(endDT);

            if (Date.parse(end) == Date.parse(start)) {
                layer.msg("结束时间不能等于开始时间！");
                return;
            }
            if (Date.parse(end) < Date.parse(start)) {
                layer.msg("结束时间不能小于开始时间！");
                return;
            }

            // 截取时间保留分钟
            var starMinutes = Number(startTime.substring(3, 5));
            var endMinutes = Number(endTime.substring(3, 5));
            if (starMinutes != endMinutes) {
                layer.msg("请假最小时长计量单位：一小时");
                return;
            }
            var arr = $("form").serializeArray();
            var data = {};
            for (var i = 0; i < arr.length; i++) {
                data[arr[i].name] = arr[i].value;
            };
            data.totalHour = $("#totalHour").val();
            data.deptCode = $("#deptCode").val();
            // 开启
            var index = layer.load(1);
            $CertusFormAjax.ajax('editOver', {
                "param": JSON.stringify(data)
                ,"stepId":stepId,"processId":processId
            }, function () {
                //关闭
                layer.close(index);
                layer.msg("申请成功");
                window.history.back()
            });
        }
    });
});
